package com.buyingtickets.controller;

import com.buyingtickets.entity.StopoverStation;
import com.buyingtickets.entity.Ticket;
import com.buyingtickets.entity.Train;
import com.buyingtickets.entity.TrainNumber;
import com.buyingtickets.service.SiteService;
import com.buyingtickets.service.StopoverStationService;
import com.buyingtickets.service.TrainNumberService;
import com.buyingtickets.service.TrainService;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.result.ModelResultMatchers;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("train")
public class TrainController {
    @Resource
    private StopoverStationService stopoverStationService;
    @Resource
    private TrainNumberService trainNumberService;
    @Resource
    private TrainService trainService;

    @RequestMapping("toTrain")

    public String toTrain(Model model) {
        return "user_trian";
    }
    /* 根据起始终点，时间 查询列车*/

    @RequestMapping(value = "getTrain", method = RequestMethod.GET)
    @ResponseBody
    public List<TrainNumber> getTrain(String siteStart, String siteEnd, String startDate) {
        System.out.println(startDate);
        if (startDate != "") {
            String startDate1 = startDate.substring(0, 10);
            System.out.println(siteStart + siteEnd + startDate);
            System.out.println(startDate1);
            List<TrainNumber> trainNumberList = trainNumberService.queryBySiteDao(siteStart, siteEnd, startDate1);
            return trainNumberList;
        }
        return null;
    }

    @RequestMapping(value = "chupiao", method = RequestMethod.GET)
    @ResponseBody
    public Ticket chupiao(char weizhi, int ck4, int price, HttpSession httpSession) {

        httpSession.getAttribute("trainNumberId");
        List<TrainNumber> trainNumberList = trainNumberService.queryBySiteDao(String.valueOf(httpSession.getAttribute("siteStart")),
                String.valueOf(httpSession.getAttribute("siteEnd"))
                , String.valueOf(httpSession.getAttribute("startDate")));
        for (int i = 0; i < trainNumberList.size(); i++) {
            if (trainNumberList.get(i).getTrainNumberId() == (int) httpSession.getAttribute("trainNumberId")){
                trainNumberList.get(i).setPrice(price);
                trainNumberList.get(i).setScol(weizhi);
                trainNumberList.get(i).setPassengerId(ck4);
                Ticket ticket = trainNumberService.queryIdChuPiao(trainNumberList.get(i));
                return ticket;
            }

        }
        return null;
    }

    @RequestMapping(value = "getexchangeTrainSreach", method = RequestMethod.GET)
    @ResponseBody
    public List<TrainNumber> getexchangeTrainSreach(String siteStart, String siteEnd, String startDate) {
        String startDate1 = startDate.substring(0, 10);
        List<TrainNumber> trainNumberList = trainNumberService.queryByHuanCheng(siteStart, siteEnd, startDate1);
        return trainNumberList;
    }

    @RequestMapping(value = "getSite", method = RequestMethod.GET)
    @ResponseBody
    public List<StopoverStation> getSite(int trainId, Model model) {

        Train train = trainService.queryByTrainID(trainId);
        System.out.println("2");
        List<StopoverStation> stopoverStationList = stopoverStationService.queryById(train);
        System.out.println("3");
        System.out.println(stopoverStationList);
        return stopoverStationList;
    }

}
